//
//  WRYNavigationController.m
//  BaiSiBuJie
//
//  Created by wry on 16/4/26.
//  Copyright © 2016年 WRY. All rights reserved.
//

#import "WRYNavigationController.h"

@interface WRYNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation WRYNavigationController
+(void)load
{
    //1.背景图片
    UINavigationBar * bar=[UINavigationBar appearanceWhenContainedIn:self, nil];
    [bar setBackgroundImage:[UIImage imageWitnOriginImageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
    //2.字体
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    dict[NSFontAttributeName]=[UIFont boldSystemFontOfSize:20];
 
    bar.titleTextAttributes=dict;
}


/*------------------全屏滑动----------------------*/
-(void)viewDidLoad
{
    [super viewDidLoad];
    //添加手势
    UIPanGestureRecognizer * pan=[[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    
    // 不允许边缘手势触发
    self.interactivePopGestureRecognizer.enabled=NO;
    // 实现滑动返回功能
    pan.delegate=self;
}
#pragma mark - UIGestureRecognizerDelegate

// 控制手势是否触发
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
     // 在根控制器下，就不需要滑动手势，返回NO
    return self.childViewControllers.count>1;
}

/*------------------全局返回按钮----------------------*/

//全局返回按钮
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if(self.childViewControllers.count)
    {//非根控制器
        //1.创建返回按钮
        UIButton *button=({
                    button=[UIButton buttonWithType:(UIButtonTypeCustom)];
            
                    //1.图片
                    [button  setImage:[UIImage imageWitnOriginImageNamed:@"navigationButtonReturn"] forState:(UIControlStateNormal)];
                    [button  setImage:[UIImage imageWitnOriginImageNamed:@"navigationButtonReturnClick"] forState:(UIControlStateHighlighted)];
                    
                    //2.标题
                    [button  setTitle:@"返回" forState:UIControlStateNormal];
                    button.titleLabel.font=[UIFont boldSystemFontOfSize:20];
                    //3.状态
                    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                    [button setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
                    [button sizeToFit];
            
                    //4.监听
                    [button addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:(UIControlEventTouchUpInside)];
            
                    //5.移动位置
    
                    button.contentEdgeInsets=UIEdgeInsetsMake(0, -25, 0, 0);
                    button;
                    });
        
        // 覆盖系统返回按钮
        viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
        //隐藏底部栏
        viewController.hidesBottomBarWhenPushed=YES;
    
    }
    
    // 3.注意:一定要记得调用super
    [super pushViewController:viewController animated:animated ];
}

//点击全局返回按
-(UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    return [super popViewControllerAnimated:YES];
}

@end
















